using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace MonoCash.ObjectModel
{
    class TransactionDetail : ObjectModel
    {
        public static TransactionDetail[] TransactionDetails
        {
            get
            {
                List<TransactionDetail> temp = new List<TransactionDetail>();
                TransactionDetail td = new TransactionDetail();
                foreach (ObjectModel o in td.Objects )
                {
                    temp.Add((TransactionDetail)o);                    
                }

                return temp.ToArray();
            }
        }
        public TransactionDetail()
            : base()
        {

        }
        public TransactionDetail(object objectId) 
            : base(objectId)
        {

        }
        public override void SyncToBackend()
        {
            if (Transaction.ID == -1)
            {
                Transaction.SyncToBackend();
            }
            base.SyncToBackend();

            decimal amount = Transaction.Amount;
            if (amount != 0)
            {
                TransactionDetail td = new TransactionDetail();
                td.TransactionID = Transaction.ID;
                td.Amount = -1 * amount;
                
            }
        }
        #region Transaction Properties
        public string Payee
        {
            get
            {
                return Transaction.Payee;
            }
            set
            {
                Transaction.Payee = value;
            }
        }
        public string Memo
        {
            get
            {
                return Transaction.Memo;
            }
            set
            {
                Transaction.Memo = value;
            }
        }
        public DateTime Date
        {
            get
            {
                return Transaction.Date;
            }
            set
            {
                Transaction.Date = value;
            }
        }
        #endregion

        public int TransactionID
        {
            get { return Transaction.ID; }
            set { transaction = new Transaction(value); }
        }
        private Transaction transaction = new Transaction();
        public Transaction Transaction
        {
            get 
            {
                return transaction;
            }
            
        }


        private int accountID = int.Parse(Program.AppSettings.UnbalancedTransactionsAccountID);
        public int AccountID
        {
            get { return accountID; }
            set {
                Account a = new Account(value);
                if (!a.AllowTransactions)
                {
                    throw new Exception(a.LongName + " does not allow transactions");
                }
                accountID = a.ID; 
                SyncToBackend(); }
        }
        public Account Account
        {
            get { return new Account(AccountID); }
            set { AccountID = value.ID; }
        }


        private int securityID = int.Parse(Program.AppSettings.DefaultSecurityID);
        public int SecurityID
        {
            get { return securityID; }
            set { securityID = value; SyncToBackend(); }
        }
        public Security Security
        {
            get { return new Security(SecurityID); }
            set { SecurityID = value.ID; }
        }

        private decimal quantity = 0;
        private bool quantityHasBeenSet = false;
        public decimal Quantity
        {
            get { return quantity; }
            set { quantity = value;
            quantityHasBeenSet = true;
                SyncToBackend(); }
        }

        public decimal Price
        {
            get
            {
                decimal price = 1;
                if (ID != -1)
                {
                    try
                    {
                        if (Quantity != 0)
                        {
                            price = Amount / Quantity / Security.ValuationFactor;
                        }
                    }
                    catch { }
                }
                return price; }
        }

        private decimal fxRate = 1;

        public decimal FxRate
        {
            get { return fxRate; }
            set { fxRate = value; SyncToBackend(); }
        }


        private int transactionDetailTypeID = int.Parse(Program.AppSettings.DefaultTransactionDetailType);
        public int TransactionDetailTypeID
        {
            get { return transactionDetailTypeID; }
            set { transactionDetailTypeID = value; SyncToBackend(); }
        }
        public TransactionDetailType TransactionDetailType
        {
            get { return new TransactionDetailType(TransactionDetailTypeID); }
            set { TransactionDetailTypeID = value.ID; }
        }

        private decimal amount = 0;
        public decimal Amount
        {
            get { return amount; }
            set 
            { 
                amount = value;
                SyncToBackend();
                if (!quantityHasBeenSet)
                {
                    Quantity = value;
                }             
            }
        }
        public TransactionDetail[] TransactionDetailOffsets
        {
            get
            {
                List<TransactionDetail> tds = new List<TransactionDetail>();

                foreach (DataRow row in Program.AppSettings.Data.TransactionDetails.Select("TransactionID=" + Transaction.ID))
                {
                    int id = int.Parse(row["ID"].ToString());
                    if (id != ID)
                    {
                        tds.Add(new TransactionDetail(id));
                    }
                }
                return tds.ToArray();
            }
        }

    }
}
